package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.StringUtils;
import com.campus.mapper.UserMapper;
import com.campus.mapping.UserMapping;
import com.campus.model.TSysUser;
import com.campus.model.dto.TSysUserCreateDto;
import com.campus.model.dto.UserSelectCondition;
import com.campus.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<UserMapper,TSysUser>
        implements ISysUserService{

    @Autowired
    private UserMapper userMapper;

    // 登录
    @Override
    public TSysUser selectUser(String username, String password) {
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("login_name", username);
        queryWrapper.eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer insertUser(TSysUserCreateDto userCreateDto) {
        return userMapper.insert(UserMapping.INSTANCE.createToUser(userCreateDto));
    }

    @Override
    public List<TSysUser> selectUserByCondition(UserSelectCondition userSelectCondition) {
        QueryWrapper<TSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(userSelectCondition.getId()),"id", userSelectCondition.getId());
        queryWrapper.like(StringUtils.isNotEmpty( userSelectCondition.getLoginName()),"login_name",
                userSelectCondition.getLoginName());

        return userMapper.selectList(queryWrapper);
    }


}
